//
//  ARAudioCell.m
//  AriseSamePro
//
//  Created by 包日守 on 15/10/28.
//  Copyright © 2015年 Arise. All rights reserved.
//

#import "ARAudioCell.h"
#import "AREdgeLabel.h"
#import <AVFoundation/AVFoundation.h>
#import "VoiceConverter.h"
#import "ARGlobalInterface.h"
#import "UIImageView+ARRessume.h"

@interface ARAudioCell ()<AVAudioPlayerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;

@property (weak, nonatomic) IBOutlet UILabel *dateLabel;

@property (weak, nonatomic) IBOutlet UIImageView *theMainImageView;

@property (weak, nonatomic) IBOutlet UILabel *likesLabel;

@property (weak, nonatomic) IBOutlet UILabel *viewsLabel;

@property (weak, nonatomic) IBOutlet UIButton *likeButton;

@property (weak, nonatomic) IBOutlet AREdgeLabel *contentLabel;
@property (nonatomic) NSArray *arrayOfFont;



@property (weak, nonatomic) IBOutlet UIButton *playButton;

//@property (nonatomic) AVPlayer *player;
@property (nonatomic) AVAudioPlayer *player;
@end


@implementation ARAudioCell


- (IBAction)shareButtonClick:(id)sender {
    
    if (self.ShareBtnBlock) {
        self.ShareBtnBlock(self.cateModel,sender);
    }
}


- (UIButton *) myLikeButton
{
    return self.likeButton;
    
}
/*
- (AVPlayer *) player
{
    if (_player == nil) {
        
        AVAudioSession *session = [AVAudioSession sharedInstance];
        [session setActive:YES error:nil];
        [session setCategory:AVAudioSessionCategoryPlayback error:nil];

        
        _player = [[AVPlayer alloc] initWithURL:[NSURL URLWithString:self.cateModel.mediaModel.stickerModel.audio]];
        
        ///Users/Arise/Downloads/2f612ffa8b43df4862937f0808606e7f_-829363562_.wav
        if ([VoiceConverter ConvertAmrToWav:@"/Users/Arise/Downloads/2f612ffa8b43df4862937f0808606e7f_-829363562_.amr" wavSavePath:@"/Users/Arise/Downloads/2f612ffa8b43df4862937f0808606e7f_-829363562_.wav"]){
            //设置label信息
            
            _player = [[AVPlayer alloc] initWithURL:[NSURL URLWithString:@"/Users/Arise/Downloads/2f612ffa8b43df4862937f0808606e7f_-829363562_.wav"]];
            
            
        }
        
       // NSLog(@"%@",self.cateModel.mediaModel.stickerModel.audio);
    }
    return _player;

}
 */

- (IBAction)palyButtonClick:(id)sender {
    
    
    
    UIButton *button = (UIButton *) sender;
    button.selected = !button.selected;
    
    
    if (button.selected) {
        //[self.streamer start];
        
        
       // [self.player play];
        
        
        
       
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSData * audioData;
            audioData = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.cateModel.mediaModel.stickerModel.audio]];
            [audioData writeToFile:[ARGlobalInterface audioAMRTempCachePath] atomically:YES];
            
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                
                if ([VoiceConverter ConvertAmrToWav:[ARGlobalInterface audioAMRTempCachePath] wavSavePath:[ARGlobalInterface audioWAVTempCachePath]])
                {
                
                    
                    NSURL *fileURL = [NSURL fileURLWithPath:[ARGlobalInterface audioWAVTempCachePath]];
                    self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
                    [self.player prepareToPlay];
                    [self.player setVolume:1];
                    //self.player.numberOfLoops = 1;
                    [self.player play];
                    self.player.delegate = self;
                    
                    //self.player = [[AVPlayer alloc] initWithURL:[NSURL fileURLWithPath:[ARGlobalInterface audioWAVTempCachePath]]];
                    //[self.player play];
                
                }
                
                
            });
            
            
            
        });
        
        
        
        
        
        
        
        //self.player = [[AVPlayer alloc] initWithURL:[NSURL fileURLWithPath:@"/Users/Arise/Downloads/2f612ffa8b43df4862937f0808606e7f_-829363562_.wav"]];
        //[self.player play];
        
        
    }
    else
    {
        self.player = nil;
    }
    
}



- (void) setCateModel:(ARCate5Model *)cateModel
{
    
    
    self.playButton.selected = NO;
    self.arrayOfFont = @[[UIFont fontWithName:@"FZCuYuan-M03S" size:26],[UIFont fontWithName:@"FZCuYuan-M03S" size:32],[UIFont fontWithName:@"FZCuYuan-M03S" size:36]];
    _cateModel = cateModel;
    
    self.contentLabel.text = cateModel.mediaModel.stickerModel.text;

    if (cateModel.mediaModel.stickerModel.text.length > 18) {
        self.contentLabel.font = self.arrayOfFont[0];

    }
    else if (cateModel.mediaModel.stickerModel.text.length < 12)
    {
        self.contentLabel.font = self.arrayOfFont[1];

    
    }
    else
    {
        self.contentLabel.font = self.arrayOfFont[2];

    }
    
    
    self.contentLabel.adjustsFontSizeToFitWidth = YES;
    
    self.iconImageView.image = nil;
    
    self.iconImageView.clipsToBounds = YES;
    self.iconImageView.layer.cornerRadius = 18;
    self.theMainImageView.image = nil;

    [self.iconImageView setImageWithURL:[NSURL URLWithString:cateModel.userModel.avatar]];
    
    //[self.theMainImageView setImageWithURL:[NSURL URLWithString:cateModel.mediaModel.stickerModel.photo]];
    
    [self.theMainImageView ar_setImageWithURL:cateModel.mediaModel.stickerModel.photo];
    
    self.nameLabel.text = cateModel.userModel.username;
    self.likesLabel.text = cateModel.likes.stringValue;
    self.viewsLabel.text = cateModel.views.stringValue;
    [self refreshLikeButton];
    
    

}


- (IBAction)likeButtonClick:(id)sender {
    
    
    [self likeButtonAction];
}


#pragma mark -avaudioplayDelegate
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{

    self.playButton.selected = NO;

}


- (void)awakeFromNib {
    // Initialization code
    
    [self.playButton setImage:[UIImage imageNamed:@"channel_audio_stop@2x"] forState:UIControlStateSelected];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
